<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            :title="popupTitle"
            :async="true"
            width="550px"
            @confirm="handleSubmit"
            @close="handleClose"
        >
            <el-form ref="formRef" :model="formData" label-width="80px" :rules="formRules">
                <el-form-item label="分类" prop="cate_id">
                    <el-select
                        class="w-[100%]"
                        v-model="formData.cate_id"
                        filterable
                        placeholder="请选择公司"
                        clearable
                    >
                        <el-option
                            v-for="item in cateList"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id"
                        />
                    </el-select>
                </el-form-item>
                <el-form-item label="名称" prop="name">
                    <el-input
                        v-model="formData.name"
                        placeholder="请输入名称"
                        clearable
                        :maxlength="100"
                    />
                </el-form-item>
                <template v-if="formData.cate_id">
                    <el-form-item label="部门" prop="dept_ids">
                        <el-tree-select
                            :props="{
                                label: 'name'
                            }"
                            node-key="id"
                            placeholder="请选择部门"
                            v-model="formData.dept_ids"
                            :data="deptList"
                            multiple
                            :render-after-expand="false"
                        />
                    </el-form-item>
                    <el-form-item label="收付款" prop="type">
                        <el-select
                            class="w-[100%]"
                            v-model="formData.type"
                            filterable
                            placeholder="请选择收付款"
                            clearable
                        >
                            <el-option
                                v-for="(item, i) in typeList"
                                :key="i"
                                :label="item"
                                :value="i"
                            />
                        </el-select>
                    </el-form-item>
                </template>
                <el-form-item label="排序" prop="sort">
                    <el-input-number v-model="formData.sort" :min="1" :max="999" />
                </el-form-item>
                <el-form-item label="是否统计" prop="is_count" v-if="!formData.cate_id">
                    <el-switch
                        v-model="formData.is_count"
                        inline-prompt
                        active-text="是"
                        inactive-text="否"
                        :active-value="1"
                        :inactive-value="0"
                    />
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>
<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
// import { apiOperationAnnexCateAdd, apiOperationAnnexCateEdit } from '@/api/operation'
// import { apiGetType } from '@/api/project'
import Popup from '@/components/popup/index.vue'
import {
    apiConfigCateAdd,
    apiConfigCateEdit,
    apiConfigCateGetAll,
    apiConfigCateTwoAdd,
    apiConfigCateTwoEdit,
    apiConfigCateTwoGettype,
    apiConfigDeptTwoGetTree
} from '@/api/config.ts'
import { cloneDeep } from 'lodash'

const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑' : '新增'
})
const formData = reactive<any>({
    id: '',
    name: '',
    cate_id: '',
    type: '',
    dept_ids: '',
    sort:'',
    is_count:1
})

const cateList = ref<any[]>([])
const typeList = ref<any[]>([])
const deptList = ref<any[]>([])

const formRules = {
    name: [
        {
            required: true,
            message: '请输入名称',
            trigger: ['blur']
        }
    ],
    type: [
        {
            required: true,
            message: '请选择收付款',
            trigger: ['change']
        }
    ],
    // dept_ids: [
    //     {
    //         required: true,
    //         message: '请选择部门',
    //         trigger: ['change']
    //     }
    // ]
}

const handleSubmit = async () => {
    await formRef.value?.validate()
    if (!formData.cate_id) {
        const data = cloneDeep(formData)
        delete data.cate_id
        delete data.dept_ids
        delete data.type
        mode.value == 'edit' ? await apiConfigCateEdit(data) : await apiConfigCateAdd(data)
    } else {
        const data = cloneDeep(formData)
        data.dept_ids = data.dept_ids.toString()
        mode.value == 'edit'
            ? await apiConfigCateTwoEdit(data)
            : await apiConfigCateTwoAdd(data)
    }

    popupRef.value?.close()
    emit('success')
}

const open = (type = 'add') => {
    mode.value = type
    getType()
    popupRef.value?.open()
}

const setFormData = (data: Record<any, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
}

const getDetail = async (row: Record<string, any>) => {
    // const data = await jobsDetail({
    //   id: row.id
    // })
    setFormData(row)
    formData.dept_ids = formData.dept_ids?.split(',')?.map((v:any)=>Number(v)) || []
}

const getType = async () => {
    cateList.value = await apiConfigCateGetAll()
    typeList.value = await apiConfigCateTwoGettype()
    deptList.value = await apiConfigDeptTwoGetTree()
}

const handleClose = () => {
    emit('close')
}

defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
